package com.kolon.business.mServiceDesk.controller;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mServiceDesk.controller.MServiceDeskController")
public class MServiceDeskController implements SimpleController {

	@SuppressWarnings("rawtypes")
	@RequestMapping(value="/business/mServiceDesk/mServiceDesk.do")
	public ModelAndView controller(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		// 도메인
		String url = MsgUtil.getMessage("xml.url");
		// 세션 아이디 및 암호 값을 불러옴
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String id = (String)userMap.get("userId");
		String pw = (String)userMap.get("userPw");
		String tid = id ;
		String assignee = "";
		String approval = "";
		StringBuffer buffer = new StringBuffer();
		buffer.append(url);			
		buffer.append("/servicedesk.aspx?mode=").append("0");
		buffer.append("&userid=").append(tid);
		// XML DOCUMENT 및 인증 처리
		Document doc = ComUtil.getDocument(buffer.toString(), id, pw);
		Element element = doc.getRootElement();
		List xmlList = element.getChildren("Table");
		Iterator itXml = xmlList.iterator();
		// XML 데이터 
		if (itXml.hasNext()) {
			Element el = (Element)itXml.next();
			assignee =  el.getChild("ASSIGNEE").getText(); //요청자
			approval =  el.getChild("APPROVAL").getText(); //승인자
		}
		//권한을 새션에 기록
		HttpSession session = request.getSession(true);
		session.setAttribute("SESSION_ASSIGNEE",  assignee);
		session.setAttribute("SESSION_APPROVAL",  approval);
		//사용자 권한에 따른 기본 페이지 처리
		String returnUrl = "";		
		if(assignee.equals("False") && approval.equals("False")){ // 요청자
			returnUrl = "/business/mServiceDesk/mServiceDeskRequest.do";
		}else if(assignee.equals("False") && approval.equals("True")){ // 승인자
			returnUrl = "/business/mServiceDesk/mServiceDeskRequestList.do";
		}else if(assignee.equals("True") && approval.equals("False")){ // 처리자
			returnUrl = "/business/mServiceDesk/mServiceDeskProcessList.do";
		}else if(assignee.equals("True") && approval.equals("True")){ // 승인자/처리자
			returnUrl = "/business/mServiceDesk/mServiceDeskRequestList.do";
		}
		// 리턴
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("assignee", assignee);
		modelAndView.addObject("approval", approval);
		modelAndView.addObject("scriptType", "009");
		modelAndView.addObject("message_url", returnUrl);
		modelAndView.setViewName("/business/common/alert");
		return modelAndView;
	}	
	
}